<?php

namespace backend\models\system;

use Yii;

/**
 * This is the model class for table "sys_config".
 *
 * @property integer $config_id
 * @property string $config_key
 * @property string $config_type
 * @property integer $config_value
 */
class SysConfig extends \yii\db\ActiveRecord
{
    //public $config_type;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%sys_config}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            //['config_type', 'integer'],
            ['config_key', 'string', 'max'=>50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'config_id'   => 'config_id',
            'config_key'  => 'config_key',
            'config_type' => 'config_type',
            'config_value'=> 'config_value',
        ];
    }

    /**
     * @inheritdoc 根据类型查询配置值
     */
    public function getValueByType()
    {
            return $this->find()
                ->select(['config_id','config_key','config_value'])
                ->where(['config_type'=>$this->config_type])
                ->asArray()->all();
    }

    /**
     * @inheritdoc 根据主键id查询配置值
     */
    public function getValueByid()
    {
        return $this->find()
            ->select(['config_id','config_key','config_type','config_value'])
            ->where(['config_id'=>$this->config_id])
            ->asArray()
	        ->one();
    }

	/**
	 * @inheritdoc 根据配置键查询配置值
	 */
	public function getValueBykey()
	{
		return $this->find()
			->select(['config_id','config_key','config_type','config_value'])
			->where(['config_key'=>$this->config_key])
			->asArray()->one();
	}

    /**
     * @inheritdoc 修改配置项信息
     */
    public static function saveValue($items, $type = null)
    {
       
        foreach ( $items as $key=>$value ) {
            $map = ['and', ['=', 'config_key', $key]];
            $type && $map[] = ['=', 'config_type', $type];
            $model = self::find()->where($map)->one();
            $model->config_value = $value;
            $model->save();
        }
    }
    /**
     * @inheritdoc 多图片上传
     */
    public function upload()
    {
        if ($this->validate()) { 
            foreach ($this->imageFiles as $file) {
	            $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
            }
            return true;
        } else {
            return false;
        }
    }

	/**
	 * @inheritdoc 修改配置项信息
	 */
	public static function saveValueid()
	{
		$items = Yii::$app->request->post("TopnavForm");
		$value = array(
			'nav_name'=>$items['nav_name'],
			'nav_url'=>$items['nav_url'],
			'nav_status'=>$items['nav_status'],
		);
		$value = serialize($value);
		$model = self::find()->where(['config_id'=>$items['config_id']])->one();
		$model->config_value = $value;
		$model->save();
	}

	/**
	 * @inheritdoc 查询全部信息
	 */
	static public function sysMessage()
	{
		return SysConfig::find()
				->asArray()
				->all();
	}
}

